Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Timer trait in embedded_hal for Channels in hardware timer #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

longfangsong
Copy link
Contributor

@longfangsong longfangsong commented Feb 13, 2021

This pr implement Timer trait in embedded_hal for each CHANNEL in a TIMER, which supports a nicer way to create delays.

@longfangsong
Copy link
Contributor Author

longfangsong commented Feb 13, 2021

Use example:

#![allow(warnings)]
#![no_std]
#![no_main]

use core::mem;
use core::sync::atomic::{compiler_fence, Ordering};
use k210_hal::fpioa;
use k210_hal::pac;
use k210_hal::pac::{sysctl, SYSCTL, TIMER0};
use k210_hal::prelude::*;
use k210_hal::stdout::Stdout;
use k210_hal::timer::Timer0;
use panic_halt as _;

#[riscv_rt::entry]
fn main() -> ! {
    let p = pac::Peripherals::take().unwrap();
    let mut sysctl = p.SYSCTL.constrain();
    // Prepare pins for UARTHS
    let fpioa = p.FPIOA.split(&mut sysctl.apb0);
    let _io5 = fpioa.io5.into_function(fpioa::UARTHS_TX);

    // Configure clocks (TODO)
    let clocks = sysctl.clocks();

    // Configure UART
    let serial = p.UARTHS.configure(115_200.bps(), &clocks);
    let (mut tx, _) = serial.split();

    // todo: new stdout design (simple Write impl?)
    let mut stdout = Stdout(&mut tx);
    let mut timer0 = sysctl.timer0;
    timer0.set_frequency(1.khz());
    let mut timer = Timer0::new(&p.TIMER0, 0, &mut timer0, &mut sysctl.apb0);
    loop {
        for _ in 0..60 {
            timer
                .try_start(1.hz())
                .unwrap_or_else(|_| writeln!(stdout, "Start failed").unwrap());
            while timer.try_wait().is_err() {
                compiler_fence(Ordering::SeqCst);
            }
        }
        writeln!(stdout, "+60s").unwrap()
    }
}

Comment on lines +8 to +9
timer: &'a $TIMER,
channel_id: usize,
Copy link
Contributor Author

@longfangsong longfangsong Feb 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should take a CHANNEL instance and provide a free function to return it back. However, I did not find a way to move a CHANNEL out of timer0::RegisterBlock::channel

@longfangsong longfangsong changed the title Implement Timer trait in embedded_hal for Channels in hardware timer. Implement Timer trait in embedded_hal for Channels in hardware timer Feb 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant